<?php

/**
 * @author chenshubo
 * 好嗨购关务订单
 */
namespace App\Service\OrderService\OrderRouteService;
use App\Service\Code\Normal;
use Service\AppService\IdService;
use App\Model\PssModel\OrderSupplier;
use App\Service\HttpServer\HttpCurl;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Service\VendorClient\ShopGwApi\ShopHhgApiClient;

class Shophhg extends BaseSupplierRouterService
{
    /**
     * 创建订单
     * @param array $param
     * @return mixed
     */
    public function createOrder( $param = [] )
    {
        //生成供应商主单号
        $supplierCode = $param['supplierGoodsInfo']['supplierCode'];
        $apiBody      = $param['supplierGoodsInfo']['apiBody'];
        $isDelare = (
            (isset($param['order']['isOrderDeclare']) && $param['order']['isOrderDeclare'] == 1) ||
            (isset($param['order']['isPayDeclare']) && $param['order']['isPayDeclare'] == 1)
            ) ? true : false;
    
        //是否云仓商城
        $isShop = isset($param['order']['isSplitOrder']) && $param['order']['isSplitOrder'] == 1 ? true : false ;
		
        //下游已申报，则使用下游的订单号作为中台子单号
        if( $isDelare && !$isShop ) {
            $param['order']['supplier_dsn'] = $param['order']['orderSn'];
        } else {
            $param['order']['supplier_dsn'] = (string)IdService::instance()->getOtherID();
        }
        
		$param['order']['supplier_psn'] = OrderSupplier::instance()->getCreateOrderSn($supplierCode);
		
        //调用上游创建订单接口
        $res = $this->callSdkApi(ShopHhgApiClient::class,$apiBody)->createOrder($param);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        
        $parameter = [
            [
                'tag'       => $supplierCode,
                'uid'       => $param['order']['supplier_dsn'],
                'url'       => $res['data']['url'],
                'data'      => json_encode($res['data']['data'],JSON_UNESCAPED_UNICODE),
                'method'    => 'post',
                'param'     => json_encode($param,JSON_UNESCAPED_UNICODE),
                'format'    => $res['data']['format'],
                'header'    => [],
            ]
        ];
        return Normal::returnCode(Normal::SUC,"获取订单参数成功",[
            'orderPush' => $parameter
        ]);
    }

    /**
     * 订单支付
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        return $this->callSdkApi(ShopHhgApiClient::class,$param['apiBody'])->payOrder($param);
    }

    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        return $this->callSdkApi(ShopHhgApiClient::class,$param['apiBody'])->cancelOrder($param);
    }

    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = [])
    {
        return $this->callSdkApi(ShopHhgApiClient::class,$param['apiBody'])->cancelOrder($param);
    }
    
    /**
     * 获取响应code
     * @param array $param
     */
    public function getCreateOrderCode( $param = [])
    {
        $res = HttpCurl::instance()->getMicorServiceResponseInfo($param);
        if( $res['code'] == Normal::SUC ) {
            return Normal::returnCode($res['code'],"OK",[
                'supplier_csn' => $res['data']['orderCode']
            ]);
        }
        return $res;
    }
}